package controller

import (
	"GoTime/models"
	"GoTime/utils"
	"net/http"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
	"xorm.io/builder"
)

func AuthCoach(c *gin.Context) {
	var (
		coach         *models.Coach
		user          *models.User
		portrait      models.Portrait
		school        models.School
		password      string
		err           error
		kemu          string
		coachloginlog *models.CoachLoginlog
		sim           string
		platenumber   string
		terminal      *models.Terminal
		tcar          models.TerminalCar
		car           *models.Car
		has           bool
	)
	if has, tcar, err = new(models.TerminalCar).GetTerCarInfoByTerSim(c.PostForm("sim")); !has && err == nil {
		c.JSON(200, gin.H{"code": 404, "msg": "终端尚未绑定", "data": nil})
		return
	}
	if terminal = new(models.Terminal).GetTerminalById(strconv.Itoa(int(tcar.TerminalId))); terminal.Id == 0 {
		c.JSON(200, gin.H{"code": 404, "msg": "请检查登录信息", "data": nil})
		return
	}
	if car, err = new(models.Car).GetOneCarById(tcar.CarId); err != nil || car.Id == 0 {
		c.JSON(200, gin.H{"code": 404, "msg": "请检查登录信息", "data": nil})
		return
	}
	if has, school, _ = new(models.School).GetSchoolById(car.SchoolId); school.Id == 0 {
		c.JSON(200, gin.H{"code": 404, "msg": "请检查登录信息", "data": nil})
		return
	}
	mobile := c.Query("mobile")
	if mobile == "" {
		c.JSON(http.StatusOK, gin.H{"code": 403, "msg": "教练手机号不能为空"})
		return
	}
	if has, coach, err = new(models.Coach).GetDeviceCoach(mobile); err != nil || !has {
		c.JSON(http.StatusOK, gin.H{"code": 403, "msg": "教练信息不存在"})
		return
	}
	if user, err = new(models.User).GetUserById(coach.UserId); user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 403, "msg": "教练信息不存在"})
		return
	}
	if has, school, err = new(models.School).GetSchoolById(coach.SchoolId); err != nil || !has {
		c.JSON(http.StatusOK, gin.H{"code": 403, "msg": "学校信息不存在"})
		return
	}
	if coach.Subject == "1" || coach.Subject == "4" {
		c.JSON(http.StatusOK, gin.H{"code": 403, "msg": "教练为理论教练不能登录终端"})
		return
	}
	if coach.SchoolId != school.Id {
		c.JSON(http.StatusOK, gin.H{"code": 403, "msg": "教练不属于本驾校"})
		return
	}
	if password = c.PostForm("password"); password != "" && user.Password != utils.Md5(password) {
		c.JSON(http.StatusOK, gin.H{"code": 403, "msg": "教练密码不正确"})
		return
	}
	if coach.Status == "1" {
		c.JSON(http.StatusOK, gin.H{"code": 403, "msg": "教练已离职"})
		return
	}
	if school.Province != "520000" {
		if coach.ApprovalStatus == "0" {
			c.JSON(http.StatusOK, gin.H{"code": 403, "msg": "该教练未审核"})
			return
		} else if coach.ApprovalStatus == "2" {
			c.JSON(http.StatusOK, gin.H{"code": 403, "msg": "该教练审核未通过"})
			return
		}
	}
	if coach.LoginStatus == "1" {
		filter := builder.NewCond()
		filter = filter.And(builder.Eq{"coach_number": coach.InstitutionCode})
		if coachloginlog, err = new(models.CoachLoginlog).GetList(filter); coachloginlog.Id == 0 {
			c.JSON(200, gin.H{"code": 403, "msg": "该教练已登录,却无登录记录", "data": nil})
			return
		}
		var terminal models.TerminalCar
		if _, terminal, _ = new(models.TerminalCar).GetTerCarInfoByTerSim(coachloginlog.Sim); terminal.Id == 0 {
			c.JSON(200, gin.H{"code": 403, "msg": "登录记录存在异常", "data": nil})
			return
		}
		var car *models.Car
		if car, err = new(models.Car).GetOneCarById(terminal.CarId); car.Id == 0 {
			c.JSON(200, gin.H{"code": 403, "msg": "该车辆不存在", "data": nil})
			return
		}
		platenumber = car.PlateNumber
		sim = c.GetString("sim")
		if sim == terminal.Sim {
			coach.LoginStatus = "0"
			if coach, err = coach.Edit("login_status"); err != nil {
				c.JSON(200, gin.H{"code": 201, "msg": "登录失败", "data": nil})
				return
			}
		} else {
			c.JSON(200, gin.H{"code": 405, "msg": "该教练已在车辆" + platenumber + "上登录", "data": nil})
			return
		}
	}
	//教练头像图片处理
	if _, portrait, _ = new(models.Portrait).GetStudentPortraitById(int64(coach.PortraitId)); portrait.Id == 0 {
		c.JSON(200, gin.H{"code": 201, "msg": "教练员没有录入头像", "data": nil})
		return
	}
	if coach.Subject == "1" {
		kemu = "一"
	} else if coach.Subject == "2" {
		kemu = "二"
	} else if coach.Subject == "3" {
		kemu = "三"
	} else {
		kemu = "四"
	}
	//根据驾校的配置来查看是否开启人脸识别的
	var (
		schoolconfig   models.SchoolNewconfig
		coafacepercent int64
		coafaceon      string
		coachlive      string
	)
	if schoolconfig, _ = new(models.SchoolNewconfig).GetConfigBySchoolId(coach.SchoolId); schoolconfig.Id == 0 {
		coafaceon = "0"
		coafacepercent = 0
		coachlive = "0"
	} else {
		coafaceon = schoolconfig.Coafaceon
		if coafaceon == "" {
			coafaceon = "0"
		}
		coafacepercent = int64(schoolconfig.Coafacepercent)
		if schoolconfig.Coachlive == "" {
			coachlive = "0"
		}
	}
	var schoolid int64
	schoolid = int64(schoolconfig.SchoolId)
	c.JSON(200, gin.H{"code": 200, "msg": "查询成功", "data": gin.H{
		"name":             user.Name,
		"photo":            portrait.ImageUrl,
		"school":           school.Name,
		"name_short":       school.NameShort,
		"phone":            strings.Replace(user.Phone, "c", "", -1),
		"id_number":        strings.Replace(user.IdNumber, "c", "", -1),
		"institution_code": coach.InstitutionCode,
		"driving_car_type": coach.TeachingCarType,
		"kemu":             kemu,
		"coafaceon":        coafaceon,
		"coafacepercent":   coafacepercent,
		"schoolid":         schoolid,
		"coachlive":        coachlive,
		"transfertype":     school.Transfertype}})
	return
}
